﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

namespace ProfilerPDS {

    public enum BoxTypes {
        [Description("Static Object")]
        StaticObject,
        [Description("Object")]
        Object,
        [Description("Execution Box")]
        ExecutionBox,
        [Description("Exception Box")]
        ExceptionBox,
        [Description("Function Box")]
        FunctionBox
    }


    class BoxShape {

        private TextBlock _txtUp;
        public TextBlock TxtUp {
            get { return _txtUp; }
        }

        private TextBlock _txtDown;
        public TextBlock TxtDown {
            get { return _txtDown; }
        }

        private Rectangle _rect;
        public Rectangle Rect {
            get { return _rect; }
        }

        private double _width;
        public double Width {
            get { return _width; }
            set { _width = value; }
        }

        private double _height;
        public double Height {
            get { return _height; }
            set { _height = value; }
        }

        private double _xPos;
        public double XPos {
            get { return _xPos; }
            set { _xPos = value; }
        }

        private double _yPos;
        public double YPos {
            get { return _yPos; }
            set { _yPos = value; }
        }

        private double _centerX;
        public double CenterX {
            get { return _centerX; }
        }

        private double _centerY;
        public double CenterY {
            get { return _centerY; }
        }

        private int _zIndex;
        public int zIndex
        {
            get { return _zIndex; }
            set { _zIndex = value; }
        }

        private string _id;
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public BoxShape(string txtUp,string txtDown,BoxTypes bTypes) {

            
            double w = 0;

            TextBlock txtU = new TextBlock();
            txtU.TextWrapping = TextWrapping.NoWrap;
            txtU.Text = txtUp;
            txtU.Foreground = Brushes.Black;
            txtU.Measure(new Size(double.PositiveInfinity,double.PositiveInfinity));

            TextBlock txtD = new TextBlock();
            txtD.TextWrapping = TextWrapping.NoWrap;
            txtD.Text = txtDown;
            txtD.Foreground = Brushes.Black;
            txtD.Measure(new Size(double.PositiveInfinity,double.PositiveInfinity));


            if (txtU.DesiredSize.Width > txtD.DesiredSize.Width) {
                w = txtU.DesiredSize.Width;
            } else {
                w = txtD.DesiredSize.Width;
            }

            Rectangle rect = new Rectangle();
            rect.Height = txtD.DesiredSize.Height + txtU.DesiredSize.Height+30;
            rect.Width = w+10;
            rect.Fill = Brushes.Azure;

            rect.Stroke = Brushes.Black;
            rect.StrokeThickness = 1;


            switch (bTypes) {
                case BoxTypes.StaticObject:
                    rect.RadiusX = 4;
                    rect.RadiusY = 4;
                    break;
                case BoxTypes.Object:
                    break;
                case BoxTypes.ExecutionBox:
                    break;
                case BoxTypes.ExceptionBox:
                    rect.RadiusX = 12;
                    rect.RadiusY = 12;
                    rect.Fill = Brushes.Orange;

                    rect.Stroke = Brushes.Red;
                    rect.StrokeThickness = 1;
                    break;
                case BoxTypes.FunctionBox:
                    rect.RadiusX = 2;
                    rect.RadiusY = 2;
                    rect.Fill = Brushes.PeachPuff;
                    rect.Stroke = Brushes.MediumVioletRed;
                    rect.StrokeThickness = 1;
                    break;
                default:
                    break;
            }

            _rect = rect;
            _txtUp = txtU;
            _txtDown = txtD;
            _width = rect.Width;
            _height = rect.Height;
            _centerX = this.XPos + this.Width / 2;
            _centerY = this.YPos + this.Height / 2;
         
        }


        public void add(double fromLeft, double fromTop, Canvas drawArea) {
            _xPos = fromLeft;
            _yPos = fromTop;

            Canvas.SetLeft(this.TxtUp, fromLeft + 5);
            Canvas.SetTop(this.TxtUp, fromTop + 6);
            Canvas.SetLeft(this.TxtDown, fromLeft + 5);
            Canvas.SetTop(this.TxtDown, fromTop + _txtUp.DesiredSize.Height + 6 + 5);

            Canvas.SetLeft(this.Rect, fromLeft);
            Canvas.SetTop(this.Rect, fromTop);

            Canvas.SetZIndex(this.Rect, _zIndex);
            Canvas.SetZIndex(this.TxtUp, _zIndex);
            Canvas.SetZIndex(this.TxtDown, _zIndex);

            drawArea.Children.Add(this.Rect);
            drawArea.Children.Add(this.TxtUp);
            drawArea.Children.Add(this.TxtDown);
        
        }


        public void add(double height, Canvas drawArea)
        {
            Canvas.SetLeft(this.TxtUp, _xPos + 5);
            Canvas.SetTop(this.TxtUp, _yPos + 6);
            Canvas.SetLeft(this.TxtDown, _xPos + 5);
            Canvas.SetTop(this.TxtDown, _yPos + _txtUp.DesiredSize.Height + 6 + 5);

            Canvas.SetLeft(this.Rect, _xPos);
            Canvas.SetTop(this.Rect, _yPos);

            Canvas.SetZIndex(this.Rect, _zIndex);
            Canvas.SetZIndex(this.TxtUp, _zIndex);
            Canvas.SetZIndex(this.TxtDown, _zIndex);

            this.Rect.Height = height + this.TxtDown.DesiredSize.Height + this.TxtUp.DesiredSize.Height + 30;
            
            drawArea.Children.Add(this.Rect);
            drawArea.Children.Add(this.TxtUp);
            drawArea.Children.Add(this.TxtDown);

        }

        public void remove(Canvas drawArea) {
            drawArea.Children.Remove(this.TxtUp);
            drawArea.Children.Remove(this.TxtDown);
            drawArea.Children.Remove(this.Rect);
        }
    
    }
}
